﻿using System.Collections.Generic;
using System.IO;
using System.Linq;

using DevExpress.XtraEditors;
using DevExpress.XtraReports.UI;
using Sumen.Business;
using Sumen.Business.Factory;
using Sumen.Business.Presenter.Sales;
using Sumen.GUI.Report.Sales;
using Sumen.GUI.Report.Sales;
using Sumen.Library.Classes.Common;
using Sumen.Library.Classes.Connection;
using Sumen.Library.Enum;
using Sumen.Library.Froms;
using Sumen.Model.Model;


namespace Sumen.GUI.Sales
{
    public partial class Frm_Confirm_Sales : XtraFormKira
    {
        private EnumFormCode _formCode;// = EnumFormCode.FrmSale;
        public Library.Enum.EnumFormCode FormCode
	{
        get { return _formCode; }
        set { _formCode = value; }
	}
        private EnumFormStatus _formStatus = EnumFormStatus.Add;

        public rptConfirmSales rptConfirm;
        private OrdersPresenter _ordersPresenter;

        string urlImageDes = ClsConnection.LocationSave;

        public Frm_Confirm_Sales(order pOrders, customer pCus)
        {
            InitializeComponent();
            
            _ordersPresenter = new OrdersPresenter();
            Orders = pOrders;
            Customer = pCus;
            LoadReport();
        }



        #region Button event

        private void buttonsArray11_btnEventAdd_Click(object sender, System.EventArgs e)
        {
            if (_formStatus == EnumFormStatus.Add)
            {
                CopyImageForOrders();
                EntityFactory.getInstance().BeginTransactionEntities();
                if (_ordersPresenter.Add(Orders) > 0)
                {
                    EntityFactory.getInstance().commit();
                    if (Orders.Ord_Payment_Status == (int)EnumOrdersStatus.Paid)
                    {
                        ClsMsgBox.ThongTin("Bán hàng thành công.");
                    }
                    else
                    {
                        ClsMsgBox.ThongTin("Đặt hàng thành công.");

                    }

                    ClsPublic.WriteLog(_formCode, EnumFormStatus.Add, Orders.Ord_Code, "");
                    IsSales = true;
                    if (chkPrint.Checked)
                    {
                        rptConfirm.Print();
                    }
                    this.Dispose();
                }
            }

        }

        private void buttonsArray1_btnEventUpdate_click(object sender, System.EventArgs e)
        {
            EntityFactory.getInstance().BeginTransactionEntities();
            if (_formStatus == EnumFormStatus.Modify)
            {
                if (_ordersPresenter.Update(Orders.orders_details.ToList(), Orders) > 0)
                {
                    EntityFactory.getInstance().commit();
                    ClsMsgBox.ThongTin("Cập nhật thành công.");
                    ClsPublic.WriteLog(_formCode, EnumFormStatus.Modify, Orders.Ord_Code, "");
                    IsSales = true;
                    this.Dispose();
                }
            }
        }

        private void buttonsArray11_btnEventClose_Click(object sender, System.EventArgs e)
        {
            this.Dispose();
        }

        private void chkPrint_CheckStateChanged(object sender, System.EventArgs e)
        {

        }

        #endregion


        #region Method

        public void LoadReport()
        {
            rptConfirm = new rptConfirmSales();

            printControl1.PrintingSystem = rptConfirm.PrintingSystem;
            SetValue_Parameter();
            rptConfirm.CreateDocument(true);
            printControl1.SetDocumentMapVisibility(true);
        }

        private void SetValue_Parameter()
        {
            rptConfirm.DataSource = rptConfirm.ConvertListToDataSet(Orders.orders_details.ToList());
            rptConfirm.DisplayName = Orders.Ord_Code;

            rptConfirm.pOrderCode.Value = Orders.Ord_Code;
            rptConfirm.pDate.Value = Orders.Ord_Date_Orders;
            rptConfirm.paraCash.Value = Orders.Ord_Cash;
            rptConfirm.paraCharge.Value = Orders.Ord_Change;
            rptConfirm.paraDiscount.Value = Orders.Ord_TotalDiscount;
            rptConfirm.pDeposit.Value = Orders.Ord_Deposit;
            //con tien dat coc
            rptConfirm.pAmount.Value = Orders.Ord_Total_Amount;
            rptConfirm.pCusName.Value = Customer.Cus_FullName;
            rptConfirm.pCusAddress.Value = Customer.Cus_Address;
            rptConfirm.pCusPhone.Value = Customer.Cus_Phone;
            rptConfirm.pCusType.Value = Customer.customer_type.CusT_Name;
        }

        //lấy đường dẫn thư muc HSLT_<HSLT_ID>
        string GetDirectoryByOrderCode(string pHoaDonCode)
        {
            return Path.Combine(urlImageDes, "HoaDon", pHoaDonCode);
        }

        //thay đổi tên file hình theo HSLT_ID
        string changeFileNameImage(string pHoaDonCode, string pProductDetailId, string _extension)
        {
            string fileNameNew = pProductDetailId + "_";
            string urltemp;
            int i = 0;
            do
            {
                i++;
                urltemp = Path.Combine(GetDirectoryByOrderCode(pHoaDonCode), pProductDetailId + i + _extension);
            }
            while (File.Exists(urltemp));

            return fileNameNew + i + _extension;
        }

        private void CopyImageForOrders()
        {
            string direction = GetDirectoryByOrderCode(Orders.Ord_Code);

            if (Orders.Ord_IsOrder != null && Orders.Ord_IsOrder == 1)
            {
                if (!Directory.Exists(direction))
                    Directory.CreateDirectory(direction);
                else
                {
                    Directory.Delete(direction);
                    Directory.CreateDirectory(direction);
                }
                List<orders_details> OrdersDetails = Orders.orders_details.ToList();
                for (int i = 0; i < OrdersDetails.Count; i++)
                {
                    string fileExtension = Path.GetExtension(OrdersDetails[i].Ords_IsOrders_Image);
                    string fileName = changeFileNameImage(Orders.Ord_Code, OrdersDetails[i].Ords_PRODUCT_DETAILS_ID, fileExtension);
                    string urlImage = Path.Combine(direction, fileName);
                    try
                    {
                        File.Copy(OrdersDetails[i].Ords_IsOrders_Image, urlImage);
                    }
                    catch
                    {
                    }

                    OrdersDetails[i].Ords_IsOrders_Image = fileName;
                }
            }
        }

        #endregion



        #region Property

        public order Orders
        {
            get;
            set;
        }

        public customer Customer
        {
            get;
            set;
        }

        public bool IsSales { get; set; }

        public EnumFormStatus FormStatus
        {
            get { return _formStatus; }
            set
            {
                _formStatus = value;
                if (_formStatus == EnumFormStatus.Add)
                {
                    buttonsArray1.btnSua.Enabled = false;
                    buttonsArray1.btnThem.Enabled = true;
                }
                else if (_formStatus == EnumFormStatus.Modify)
                {
                    buttonsArray1.btnSua.Enabled = true;
                    buttonsArray1.btnThem.Enabled = false;
                }
                else if (_formStatus == EnumFormStatus.Print)
                {
                    buttonsArray1.btnSua.Enabled = false;
                    buttonsArray1.btnThem.Enabled = false;
                }
            }
        }

        #endregion





    }
}